package com.leetcode.hot100;

import java.util.Arrays;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.hot100
 * @Author: lei
 * @CreateTime: 2024-03-27  16:04
 * @Description: 给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。
 * @Version: 1.0
 */
public class 轮转数组 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7};
        //int[] nums = {-1, -100, 3, 99};
        int k = 3;
        rotate(nums, k);
    }

    public static void rotate(int[] nums, int k) {
        int length = nums.length;
        k = k % length;
        int index = length - k;
        int[] temp = new int[k];
        System.arraycopy(nums, index, temp, 0, k);


        for (int i = length - k - 1; i >= 0; i--) {
            nums[i + k] = nums[i];
        }
        System.arraycopy(temp, 0, nums, 0, k);
        System.out.println(Arrays.toString(nums));
    }
// 超时
/*    public static void rotate(int[] nums, int k) {
        int length = nums.length;
        k = k > length ? (k % length) : k;
        int end = length - 1;
        for (int j = 0; j < k; j++) {
            int temp = nums[end];
            for (int i = end - 1; i >= 0; i--) {
                nums[end] = nums[i];
                end--;
            }
            end = length - 1;
            nums[0] = temp;
        }
        System.out.println(Arrays.toString(nums));
    }*/

}
